# Windows Make
#
# Powershell does not allow you to execute ps1 scripts in default,
# run this command as administrator in powershell:
# `Set-ExecutionPolicy RemoteSigned`
#
# Author: xczh <xczh.me@foxmail.com>
#

param(
[int]$CGO_ENABLED=(go env CGO_ENABLED),
[switch]$build,
[switch]$gox_build
)

$ver = git describe --tags
$githash = git rev-parse HEAD
$builddate = Get-Date -UFormat '%Y/%m/%d %H:%M:%S'

$exec = "jetserver"

$go_output = "build/"

$gox_output = "gox_build/"
$gox_arch = "386 amd64"
$gox_os = "windows linux"
$gox_osarch = "darwin/amd64 linux/arm"

$LDFLAGS = "-s -w -X 'main.Version=${ver}' -X 'main.Builddate=${builddate}' -X main.Buildhash=${githash}"

# for single build
if($build)
{
    "[build] Delete '${go_output}' dir ..."
    Remove-Item $go_output -Recurse -Force -ErrorAction SilentlyContinue
    $env:CGO_ENABLED=$CGO_ENABLED
    "[build] Build use CGO_ENABLED=$CGO_ENABLED, -ldflags=$LDFLAGS"
    go build -o "${go_output}/${exec}.exe" -ldflags "$LDFLAGS"
    "[build] Finish."
}

# for release build
if($gox_build)
{
    "[gox_build] Delete '${gox_output}' dir ..."
    Remove-Item $gox_output -Recurse -Force -ErrorAction SilentlyContinue
    $env:CGO_ENABLED=$CGO_ENABLED
    "[gox_build] Build use CGO_ENABLED=$CGO_ENABLED, -ldflags=$LDFLAGS"
    gox -arch="${gox_arch}" -ldflags="${LDFLAGS}" -os="${gox_os}" -osarch="${gox_osarch}" -output="${gox_output}/${exec}_{{.OS}}_{{.Arch}}" -verbose .
    "[gox_build] Finish."
}